package com.example.bannerdemo;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;

import com.example.bannerdemo.bean.Banner;
import com.example.bannerdemo.view.BannerAdapter;
import com.example.bannerdemo.view.BannerAdapter.OnItemClickListener;
import com.example.bannerdemo.view.BannerView;

public class MainActivity extends Activity implements OnItemClickListener {

	private BannerView mBannerView;
	private BannerAdapter mAdapter;
	private List<Banner> mData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();

		mData = new ArrayList<Banner>();
		mAdapter = new BannerAdapter(this, mData, this);
		mBannerView.setAdapter(mAdapter);

		// 添加数据后通知数据改变了需要更新
		mData.addAll(getData());
		mBannerView.notifyDataSetChanged();
	}

	private void initView() {
		mBannerView = (BannerView) findViewById(R.id.banner1);

	}

	public void onClick(View view) {
		new GetDataTask().execute();
	}

	private final class GetDataTask extends
			AsyncTask<Void, Integer, List<Banner>> {

		@Override
		protected List<Banner> doInBackground(Void... params) {
			// 获取新的数据操作
			return getNewData();
		}

		private List<Banner> getNewData() {
			List<Banner> data = new ArrayList<Banner>();

			Banner banner5 = new Banner();
			banner5.setId("banner_id_5");
			banner5.setName("banner_name_5");
			banner5.setTitle("banner_title_5");
			banner5.setUrl("http://www.baidu.com");
			banner5.setImg("https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/news/q=100/sign=ac2091b849086e066ca83b4b32097b5a/d8f9d72a6059252dc093108d319b033b5ab5b9b6.jpg");
			data.add(banner5);

			Banner banner1 = new Banner();
			banner1.setId("banner_id_6");
			banner1.setName("banner_name_6");
			banner1.setTitle("banner_title_6");
			banner1.setUrl("http://www.baidu.com");
			banner1.setImg("https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/news/q=100/sign=4384d8a6bb096b6387195a503c328733/4a36acaf2edda3cc351ba7a904e93901203f92bf.jpg");
			data.add(banner1);

			Banner banner2 = new Banner();
			banner2.setId("banner_id_7");
			banner2.setName("banner_name_7");
			banner2.setTitle("banner_title_7");
			banner2.setUrl("http://www.baidu.com");
			banner2.setImg("https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=f55dc7152bdda3cc0bb1eb6067d40d33/622762d0f703918f1e876340543d269758eec4e2.jpg");
			data.add(banner2);

			try {
				// 模拟耗时任务
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

			return data;
		}

		@Override
		protected void onPreExecute() {
			System.out.println("加载开始");

		}

		@Override
		protected void onPostExecute(List<Banner> result) {
			System.out.println("加载完毕...");
			// 添加数据后通知数据改变了需要更新
			mData.clear();
			mData.addAll(result);
			mBannerView.notifyDataSetChanged();
		}
	}

	/**
	 * 模拟获取数据
	 */
	private List<Banner> getData() {
		List<Banner> data = new ArrayList<Banner>();
		Banner banner1 = new Banner();
		banner1.setId("banner_id_1");
		banner1.setName("banner_name_1");
		banner1.setTitle("banner_title_1");
		banner1.setUrl("http://www.baidu.com");
		banner1.setImg("https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=cad210339b510fb3784c24d7bf0efca7/9825bc315c6034a872c0e60bce1349540923761d.jpg");
		data.add(banner1);

		Banner banner2 = new Banner();
		banner2.setId("banner_id_2");
		banner2.setName("banner_name_2");
		banner2.setTitle("banner_title_2");
		banner2.setUrl("http://www.baidu.com");
		banner2.setImg("https://ss0.baidu.com/94o3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=d94aa043d22a60595245b25a4e0900a9/b8014a90f603738d213240a8b61bb051f919ecc4.jpg");
		data.add(banner2);

		Banner banner3 = new Banner();
		banner3.setId("banner_id_3");
		banner3.setName("banner_name_3");
		banner3.setTitle("banner_title_3");
		banner3.setUrl("http://www.baidu.com");
		banner3.setImg("https://ss3.baidu.com/9fo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=80749fa5bb096b63814c0d106a0eb376/78310a55b319ebc43d772e138726cffc1e171628.jpg");
		data.add(banner3);

		Banner banner4 = new Banner();
		banner4.setId("banner_id_4");
		banner4.setName("banner_name_4");
		banner4.setTitle("banner_title_4");
		banner4.setUrl("http://www.baidu.com");
		banner4.setImg("https://ss2.baidu.com/-vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=cb62208ca9af2eddd4a41aa9eb2d35db/8b13632762d0f7032ce4cb560dfa513d2797c5e2.jpg");
		data.add(banner4);

		Banner banner5 = new Banner();
		banner5.setId("banner_id_5");
		banner5.setName("banner_name_5");
		banner5.setTitle("banner_title_5");
		banner5.setUrl("http://www.baidu.com");
		banner5.setImg("https://ss1.baidu.com/-4o3dSag_xI4khGko9WTAnF6hhy/news/q=100/sign=ac2091b849086e066ca83b4b32097b5a/d8f9d72a6059252dc093108d319b033b5ab5b9b6.jpg");
		data.add(banner5);

		return data;
	}

	@Override
	public void onItemClick(Banner banner) {
		System.out.println("onItemClick" + "\n" + banner);

	}

}
